<?php
/**
 * Created by PhpStorm.
 * User: cgx
 * Date: 2016/8/3
 * Time: 19:16
 */
namespace spider\Controller;
use Think\Controller;
use QL\QueryList;
use Sunra\PhpSimple\HtmlDomParser;
set_time_limit(0);
error_reporting(0);

class LongzhuController extends Controller{

    public function index(){
        $start_time = time();

        $game_type = C('LONGZHU_GAME');
        $page_size = 30;
        $page = 0;
        $top_views = 100;
        /*设置一个开关，当第一次更新直播表的时候，将所有记录中的isOnlive设置为0，然后在进行更新,
                                 接下来更新的时候就不用更新isOnlive字段了
                               */
        $is_update = true;
        while ($page < 100){

            $start_index = $page * $page_size;
            $longzhu_api = sprintf(C('LONGZHU_API'),$page_size,$start_index);
            echo $longzhu_api;
            //获得api中的json
            $response = QueryList::Query($longzhu_api,array('html'=>'html'))->html;
            if($response && strlen($response) != 0){
                $res_arr = json_decode($response,true);
                if($res_arr['data']['totalItems']==0){
                    break;
                }
                $res_list = $res_arr['data']['items'];


                //获得单个直播接相关信息
                foreach ($res_list as $k=> $v){


                    $gameHostName = $v['game'][0]['tag'];
                    if(!array_key_exists($gameHostName,$game_type)){
                        continue;
                    }
                    if($top_views > $v['viewers']){
                        continue;
                    }
                    $views = $v['viewers'];
                    $room_id = $v['channel']['id'];
                    $vid = $v['channel']['vid'];


                    $kw['title'] = $v['channel']['status'];
                    $kw['thumb'] = $v['preview'];
                    $kw['keywords'] ='';
                    $kw['description'] = '';
                    $kw['inputtime'] = time();
                    $kw['updatetime'] = time();
                    $kw['zb_thumb'] = $v['channel']['avatar'];
                    $kw['zbid'] = $v['channel']['domain'];
                    $kw['zbname'] = $v['channel']['name'];
                    $kw['source'] = 'longzhu';
                    $kw['category'] = $game_type[$gameHostName];
                    $kw['isOnlive'] = '1';
                    $kw['livedata'] = $this->get_zb_room_str($vid,$room_id);
                    $kw['m_livedata'] = '';
                    $kw['views'] = $views;

                    if($is_update){
                        befor_update('longzhu');
                        update_zhibo('longzhu',$kw);
                        $is_update=false;
                    }else{
                        update_zhibo('longzhu',$kw);
                    }
                }
            }

            $page+=1;
        }


        echo 'time cost '.(time()-$start_time).' seconds';
    }


    public function get_zb_room_str($vid,$room_id){
        if(intval($vid)==0){
            return 'http://r.plures.net/proton/flash/streaming-ifp2rgic.swf?hasNextBtn=0&amp;hasMovieBtn=0&amp;autoPlay=1&amp;roomId='.strval($room_id);
        }else{
            return 'http://imgcache.qq.com/minivideo_v1/vd/res/TencentPlayerLive.swf?max_age=86400&amp;vid='.strval($vid).'&amp;vurl=http://zb.v.qq.com:1863/?progid='.strval($vid) . '&amp;showcfg=1&amp;share=1&amp;full=1&amp;autoplay=1&amp;scale=full&amp;p=true&amp;loadingswf=http://imgcache.qq.com/minivideo_v1/vd/res/skins/longzhu_loading.swf&amp;';
        }

    }
}